"""
author: CAIGUI LIN
date: 2024.12.8
"""
import numpy as np
import math
from datetime import datetime
import os

def get_unique_filename():
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S%f")
    return f"{timestamp}"

def get_size(width, height, w_ratio, h_ratio, interval):
    """
    :param width: 输入图片的宽度
    :param height: 输入图片的高度
    :param w_ratio: 宽度的缩放比例
    :param h_ratio: 高度的缩放比例
    :param interval: 缩放的步长
    :return: 缩放后的图片的宽度和高度
    """
    low_width, high_width = int((1 - w_ratio) * width), int((1 + w_ratio) * width)
    low_height, high_height = int((1 - h_ratio) * height), int((1 + h_ratio) * height)
    width_lst = range(low_width, high_width,  interval)
    height_lst = range(low_height, high_height, interval)
    return [(i, j) for i in width_lst for j in height_lst]
def retangle_scale(width, height, low, high, interval):
    """""
      :param width: 输入图片的宽度
      :param height: 输入图片的高度
      :param low: 宽高度的缩放比例
      :param high: 宽高度的缩放比例
      :param interval: 缩放的步长
      :return: 缩放后的图片的宽度和高度
      """
    low_width, low_height = int(width * low), int(height * low)
    high_width, high_height = int(width  * (1 + high)), int(height * (1 + high))
    width_lst = range(low_width, high_width,  int((high_width - low_width) * interval))
    height_lst = range(low_height, high_height, int((high_height - low_height) * interval))
    return [(i, j) for i, j in zip(width_lst, height_lst)] 
def calculate_distance(p1, p2, scaled_x, scaled_y):
    """ 计算两个点之间的欧几里得距离 """
    return math.sqrt(((p1.x - p2.x) * scaled_x) ** 2 + ((p1.y - p2.y) * scaled_y) ** 2 + (p1.z - p2.z) ** 2)
def position_project(min_x, min_y, max_x, max_y, hand_landmarks):
    src_width = abs(max_x - min_x)
    src_height = abs(max_y - min_y)
    distances = []
    indices = []
    sizes = get_size(src_width, src_height, 0.1, 0.1, 10)# 0.9-1.1
    for i in range(len(sizes)):
        retangles = retangle_scale(sizes[i][0], sizes[i][1], 0.8, 0.2, 0.1)# 0.8 - 1.2
        for j in range(len(retangles)):
            aug_width = retangles[j][0]
            aug_height = retangles[j][1]
            x_ratio = aug_width / src_width #宽度缩放比例
            y_ratio = aug_height / src_height #高度缩放比例
            """ 计算 hand_landmarks 中所有点两两之间的距离 """
            landmarks = hand_landmarks.landmark
            num_landmarks = len(landmarks)
            dis = []
            for k in range(num_landmarks):
                for h in range(k + 1, num_landmarks):
                    distance = calculate_distance(landmarks[k], landmarks[h], x_ratio, y_ratio)
                    dis.append(distance)
            indices.append(get_unique_filename())
            distances.append(dis)
    return np.array(distances), indices
if __name__ == '__main__':
    pass
    # print(get_size(1280, 720, 0.1, 0.1, 1))
    # print(retangle_scale(1280, 720, 0.5, 0.5, 0.05))